package org.feiye.studyday28.case4;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class LoginClient {
//基于UDP协议的Socket网络编程步骤，客户端
	public static void main(String[] args) {
		// 买礼物
		String info = "巧克力蛋糕";
		byte[] bytes = info.getBytes();
		// 对方的地址和端口号
		InetAddress ia;
		// 快递点
		DatagramSocket socket = null;
		try {
			ia = InetAddress.getByName("localhost");
			// 包装礼物
			DatagramPacket dg = new DatagramPacket(bytes, bytes.length, ia, 5000);
			socket = new DatagramSocket();
			// 通过快递点寄礼物
			socket.send(dg);

			// 接收服务器响应
			byte[] replys = new byte[1024];
			DatagramPacket dg1 = new DatagramPacket(replys, replys.length);
			socket.receive(dg1);
			// 拆礼物
			String reply = new String(dg1.getData(), 0, dg1.getData().length);
			System.out.println("服务器响应：" + reply);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (socket != null)
				socket.close();
		}

	}
}
